package com.itaming.lycheeframework.jackson.modules;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.itaming.lycheeframework.common.constant.FrameworkConstant;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Java8日期时间模块
 *
 * @author A.Ming
 */
public class Jsr310DateTimeModule extends SimpleModule {

    public Jsr310DateTimeModule() {
        addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(FrameworkConstant.DATE_FORMAT)));
        addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(FrameworkConstant.TIME_FORMAT)));
        addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(FrameworkConstant.DATETIME_FORMAT)));
        addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(FrameworkConstant.DATE_FORMAT)));
        addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(FrameworkConstant.TIME_FORMAT)));
        addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(FrameworkConstant.DATETIME_FORMAT)));
    }

}
